﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Editor;
using ncgmpToolbar.Utilities;
using ESRI.ArcGIS.Geometry;

namespace ncgmpToolbar.Utilities.DataAccess
{
    class ConcealedContactsAndFaultsAccess
    {
        IFeatureClass m_ConcealedContactsAndFaultsFC;
        IWorkspace m_theWorkspace;

        public ConcealedContactsAndFaultsAccess(IWorkspace theWorkspace)
        {
            m_ConcealedContactsAndFaultsFC = commonFunctions.OpenFeatureClass(theWorkspace, "ConcealedContactsAndFaults");
            m_theWorkspace = theWorkspace;            
        }

        public struct ConcealedContactsAndFault
        {
            public string ConcealedContactsAndFaults_ID;
            public string Type;
            public double LocationConfidenceMeters;
            public string ExistenceConfidence;
            public string IdentityConfidence;
            public string Symbol;
            public string Label;
            public string Notes;
            public string DataSourceID;
            public IPolyline Shape;
            public bool RequiresUpdate;
        }        

        private Dictionary<string, ConcealedContactsAndFault> m_ConcealedContactsAndFaultsDictionary = new Dictionary<string, ConcealedContactsAndFault>();
        
        public Dictionary<string, ConcealedContactsAndFault> ConcealedContactsAndFaultsDictionary
        {
            get { return m_ConcealedContactsAndFaultsDictionary; }
        }

        public void ClearConcealedContactsAndFaults()
        {
            m_ConcealedContactsAndFaultsDictionary.Clear();
        }

        public void AddConcealedContactsAndFaults(string SqlWhereClause)
        {
            int idFld = m_ConcealedContactsAndFaultsFC.FindField("ConcealedContactsAndFaults_ID");
            int typeFld = m_ConcealedContactsAndFaultsFC.FindField("Type");
            int locConfFld = m_ConcealedContactsAndFaultsFC.FindField("LocationConfidenceMeters");
            int exConfFld = m_ConcealedContactsAndFaultsFC.FindField("ExistenceConfidence");
            int idConfFld = m_ConcealedContactsAndFaultsFC.FindField("IdentityConfidence");
            int lblFld = m_ConcealedContactsAndFaultsFC.FindField("Label");
            int notesFld = m_ConcealedContactsAndFaultsFC.FindField("Notes");
            int dsFld = m_ConcealedContactsAndFaultsFC.FindField("DataSourceID");
            int symFld = m_ConcealedContactsAndFaultsFC.FindField("Symbol");

            IQueryFilter QF = new QueryFilterClass();
            QF.WhereClause = SqlWhereClause;

            IFeatureCursor theCursor = m_ConcealedContactsAndFaultsFC.Search(QF, false);
            IFeature theFeature = theCursor.NextFeature();

            while (theFeature != null)
            {
                ConcealedContactsAndFault anConcealedContactsAndFault = new ConcealedContactsAndFault();
                anConcealedContactsAndFault.ConcealedContactsAndFaults_ID = theFeature.get_Value(idFld).ToString();
                anConcealedContactsAndFault.Type = theFeature.get_Value(typeFld).ToString();
                anConcealedContactsAndFault.LocationConfidenceMeters = double.Parse(theFeature.get_Value(locConfFld).ToString());
                anConcealedContactsAndFault.ExistenceConfidence = theFeature.get_Value(exConfFld).ToString();
                anConcealedContactsAndFault.IdentityConfidence = theFeature.get_Value(idConfFld).ToString();
                anConcealedContactsAndFault.Label = theFeature.get_Value(lblFld).ToString();
                anConcealedContactsAndFault.Notes = theFeature.get_Value(notesFld).ToString();
                anConcealedContactsAndFault.DataSourceID = theFeature.get_Value(dsFld).ToString();
                anConcealedContactsAndFault.Symbol = theFeature.get_Value(symFld).ToString();
                anConcealedContactsAndFault.Shape = (IPolyline)theFeature.Shape;
                anConcealedContactsAndFault.RequiresUpdate = true;

                m_ConcealedContactsAndFaultsDictionary.Add(anConcealedContactsAndFault.ConcealedContactsAndFaults_ID, anConcealedContactsAndFault);

                theFeature = theCursor.NextFeature();
            }
        }

        public string NewConcealedContactsAndFault(string Type, double LocationConfidenceMeters, string ExistenceConfidence, string IdentityConfidence,
            string Label, string Notes, string DataSourceID, string Symbol, IPolyline Shape)
        {
            ConcealedContactsAndFault newConcealedContactsAndFault = new ConcealedContactsAndFault();

            sysInfo SysInfoTable = new sysInfo(m_theWorkspace);
            newConcealedContactsAndFault.ConcealedContactsAndFaults_ID = SysInfoTable.ProjAbbr + ".ConcealedContactsAndFaults." + SysInfoTable.GetNextIdValue("ConcealedContactsAndFaults");
            newConcealedContactsAndFault.Type = Type;
            newConcealedContactsAndFault.LocationConfidenceMeters = LocationConfidenceMeters;
            newConcealedContactsAndFault.ExistenceConfidence = ExistenceConfidence;
            newConcealedContactsAndFault.IdentityConfidence = IdentityConfidence;
            newConcealedContactsAndFault.Label = Label;
            newConcealedContactsAndFault.Notes = Notes;
            newConcealedContactsAndFault.DataSourceID = DataSourceID;
            newConcealedContactsAndFault.Symbol = Symbol;
            newConcealedContactsAndFault.Shape = Shape;
            newConcealedContactsAndFault.RequiresUpdate = false;

            m_ConcealedContactsAndFaultsDictionary.Add(newConcealedContactsAndFault.ConcealedContactsAndFaults_ID, newConcealedContactsAndFault);
            return newConcealedContactsAndFault.ConcealedContactsAndFaults_ID;
        }

        public void UpdateConcealedContactsAndFault(ConcealedContactsAndFault theConcealedContactsAndFault)
        {
            try { m_ConcealedContactsAndFaultsDictionary.Remove(theConcealedContactsAndFault.ConcealedContactsAndFaults_ID); }
            catch { }

            theConcealedContactsAndFault.RequiresUpdate = true;
            m_ConcealedContactsAndFaultsDictionary.Add(theConcealedContactsAndFault.ConcealedContactsAndFaults_ID, theConcealedContactsAndFault);
        }

        public void SaveConcealedContactsAndFaults()
        {
            int idFld = m_ConcealedContactsAndFaultsFC.FindField("ConcealedContactsAndFaults_ID");
            int typeFld = m_ConcealedContactsAndFaultsFC.FindField("Type");
            int locConfFld = m_ConcealedContactsAndFaultsFC.FindField("LocationConfidenceMeters");
            int exConfFld = m_ConcealedContactsAndFaultsFC.FindField("ExistenceConfidence");
            int idConfFld = m_ConcealedContactsAndFaultsFC.FindField("IdentityConfidence");
            int lblFld = m_ConcealedContactsAndFaultsFC.FindField("Label");
            int notesFld = m_ConcealedContactsAndFaultsFC.FindField("Notes");
            int dsFld = m_ConcealedContactsAndFaultsFC.FindField("DataSourceID");
            int symFld = m_ConcealedContactsAndFaultsFC.FindField("Symbol");

            IEditor theEditor = ArcMap.Editor;
            if (theEditor.EditState == esriEditState.esriStateNotEditing) { theEditor.StartEditing(m_theWorkspace); }
            theEditor.StartOperation();

            try
            {
                string updateWhereClause = "ConcealedContactsAndFaults_ID = '";
                IFeatureCursor insertCursor = m_ConcealedContactsAndFaultsFC.Insert(true);

                foreach (KeyValuePair<string, ConcealedContactsAndFault> aDictionaryEntry in m_ConcealedContactsAndFaultsDictionary)
                {
                    ConcealedContactsAndFault thisConcealedContactsAndFault = (ConcealedContactsAndFault)aDictionaryEntry.Value;
                    switch (thisConcealedContactsAndFault.RequiresUpdate)
                    {
                        case true:
                            updateWhereClause += thisConcealedContactsAndFault.ConcealedContactsAndFaults_ID + "' OR ConcealedContactsAndFaults_ID = '";
                            break;

                        case false:
                            IFeatureBuffer theFeatureBuffer = m_ConcealedContactsAndFaultsFC.CreateFeatureBuffer();
                            theFeatureBuffer.set_Value(idFld, thisConcealedContactsAndFault.ConcealedContactsAndFaults_ID);
                            theFeatureBuffer.set_Value(typeFld, thisConcealedContactsAndFault.Type);
                            theFeatureBuffer.set_Value(locConfFld, thisConcealedContactsAndFault.LocationConfidenceMeters);
                            theFeatureBuffer.set_Value(exConfFld, thisConcealedContactsAndFault.ExistenceConfidence);
                            theFeatureBuffer.set_Value(idConfFld, thisConcealedContactsAndFault.IdentityConfidence);
                            theFeatureBuffer.set_Value(lblFld, thisConcealedContactsAndFault.Label);
                            theFeatureBuffer.set_Value(notesFld, thisConcealedContactsAndFault.Notes);
                            theFeatureBuffer.set_Value(dsFld, thisConcealedContactsAndFault.DataSourceID);
                            theFeatureBuffer.set_Value(symFld, thisConcealedContactsAndFault.Symbol);
                            theFeatureBuffer.Shape = thisConcealedContactsAndFault.Shape;

                            insertCursor.InsertFeature(theFeatureBuffer);
                            break;
                    }
                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(insertCursor);
                theEditor.StopOperation("Insert ConcealedContactsAndFaults");

                if (updateWhereClause == "ConcealedContactsAndFaults_ID = '") { return; }

                theEditor.StartOperation();
                updateWhereClause = updateWhereClause.Remove(updateWhereClause.Length - 37);

                IQueryFilter QF = new QueryFilterClass();
                QF.WhereClause = updateWhereClause;

                IFeatureCursor updateCursor = m_ConcealedContactsAndFaultsFC.Update(QF, false);
                IFeature theFeature = updateCursor.NextFeature();

                while (theFeature != null)
                {
                    string theID = theFeature.get_Value(idFld).ToString();

                    ConcealedContactsAndFault thisConcealedContactsAndFault = m_ConcealedContactsAndFaultsDictionary[theID];
                    theFeature.set_Value(typeFld, thisConcealedContactsAndFault.Type);
                    theFeature.set_Value(locConfFld, thisConcealedContactsAndFault.Type);
                    theFeature.set_Value(exConfFld, thisConcealedContactsAndFault.ExistenceConfidence);
                    theFeature.set_Value(idConfFld, thisConcealedContactsAndFault.IdentityConfidence);
                    theFeature.set_Value(lblFld, thisConcealedContactsAndFault.Label);
                    theFeature.set_Value(notesFld, thisConcealedContactsAndFault.Notes);
                    theFeature.set_Value(dsFld, thisConcealedContactsAndFault.DataSourceID);
                    theFeature.set_Value(symFld, thisConcealedContactsAndFault.Symbol);
                    theFeature.Shape = thisConcealedContactsAndFault.Shape;
                    updateCursor.UpdateFeature(theFeature);

                    theFeature = updateCursor.NextFeature();
                }

                theEditor.StopOperation("Update ConcealedContactsAndFaults");
            }
            catch { theEditor.StopOperation("ConcealedContactsAndFaults Management Failure"); }
        }

        public void DeleteConcealedContactsAndFaults(ConcealedContactsAndFault theConcealedContactsAndFault)
        {
            try { m_ConcealedContactsAndFaultsDictionary.Remove(theConcealedContactsAndFault.ConcealedContactsAndFaults_ID); }
            catch { }

            IEditor theEditor = ArcMap.Editor;
            if (theEditor.EditState == esriEditState.esriStateNotEditing) { theEditor.StartEditing(m_theWorkspace); }
            theEditor.StartOperation();

            try
            {
                IQueryFilter QF = new QueryFilterClass();
                QF.WhereClause = "ConcealedContactsAndFaults_ID = '" + theConcealedContactsAndFault.ConcealedContactsAndFaults_ID + "'";

                ITable ConcealedContactsAndFaultsTable = m_ConcealedContactsAndFaultsFC as ITable;
                ConcealedContactsAndFaultsTable.DeleteSearchedRows(QF);

                theEditor.StopOperation("Delete ConcealedContactsAndFaults");
            }
            catch (Exception e) { theEditor.StopOperation("ConcealedContactsAndFaults Management Failure"); }
        }
    }
}
